/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package marsaMaroc.controllers;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import marsaMaroc.dao.ClientDAO;
import marsaMaroc.dao.FactureDAO;
import marsaMaroc.dao.ReclamationDAO;
import marsaMaroc.model.Client;
import marsaMaroc.model.Facture;
import marsaMaroc.model.Reclamation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Administrateur
 */
@Controller
public class reclamationController {

    ReclamationDAO reclamationtDAO = new ReclamationDAO();

    @RequestMapping(value = "/addReclamation.htm", method = RequestMethod.POST)
    protected ModelAndView add(
            @RequestParam("type") String type,
            @RequestParam("motif") String motif,
            @RequestParam("date") String date,
            @RequestParam("statut") String statut,
            @RequestParam("facture") int facture
    ) throws Exception {

        Reclamation rec = new Reclamation();
        rec.setType(type);
        rec.setMotif(motif);
        rec.setDateCreation(new Date(date));
        rec.setStatut(statut);
        rec.setFacture(new FactureDAO().get(facture));

        reclamationtDAO.add(rec);
        return new ModelAndView("client_crud/list_rec");
    }

    @RequestMapping(value = "/newReclamation.htm", method = RequestMethod.GET)
    protected ModelAndView newRec(HttpServletRequest request) throws Exception {

        Client c = (Client) request.getSession().getAttribute("client");
        List<Facture> listF = new ClientDAO().getAllFactures(c);
        return new ModelAndView("client_side/add_reclamation", "listF", listF);
    }

    @RequestMapping(value = "/deleteReclamation.htm", method = RequestMethod.POST)
    protected ModelAndView delete(
            @RequestParam("id") int id
    ) throws Exception {

        Reclamation c = (Reclamation) reclamationtDAO.get(id);
        reclamationtDAO.delete(c);
        return new ModelAndView("index");
    }

    @RequestMapping(value = "/listReclamation.htm", method = RequestMethod.GET)
    protected ModelAndView getAll(HttpServletRequest request) throws Exception {

        List<Reclamation> listR = reclamationtDAO.getAll();
        System.out.println("sizeReclamations " + listR.size());
        request.getSession().setAttribute("listeR", listR);
        return new ModelAndView("client_crud/list_rec");
    }

}
